home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / benchmarks / wrblocks / wrblocks.c < prev   
Encoding:
C/C++ Source or Header  |  1990-08-24  |  1.7 KB  |  79 lines

  1. /*
  2.  * wrblocks - create a file by writing a specified number of blocks to it.
  3.  */
  4. #include <sys/file.h>
  5. #include "fs.h"
  6. #include "stdio.h"
  7. #include "option.h"
  8. #include "errno.h"
  9.  
  10. /*
  11.  * Options set by command line arguments
  12.  */
  13. int numKbytes = 10;
  14. int blockSize = 16;
  15.  
  16. Option optionArray[] = {
  17.     OPT_INT,  "k", (Address)&numKbytes, "Number of Kbytes for the file",
  18.     OPT_INT,  "b", (Address)&blockSize, "Blocksize of writes, in Kbytes",
  19. };
  20. int numOptions = sizeof(optionArray) / sizeof(Option);
  21.  
  22.  
  23. /*
  24.  *----------------------------------------------------------------------
  25.  *
  26.  * main --
  27.  *
  28.  *    Open the specified file and writes numKbytes 1K blocks to it.
  29.  *
  30.  * Results:
  31.  *    None.
  32.  *
  33.  * Side effects:
  34.  *    Writes the file.
  35.  *
  36.  *----------------------------------------------------------------------
  37.  */
  38. main(argc, argv)
  39.     int argc;
  40.     char *argv[];
  41. {
  42.     int openFileID;
  43.     char *fileName;
  44.     char *bufPtr;
  45.     int bytesWritten;
  46.     int k;
  47.  
  48.     argc = Opt_Parse(argc, argv, optionArray, numOptions, 0);
  49.  
  50.     if (argc < 2) {
  51.     openFileID = 1;
  52.     } else {
  53.     fileName = argv[1];
  54.  
  55.     openFileID = open(fileName, O_CREAT|O_WRONLY, 0666);
  56.     if (openFileID < 0) {
  57.         fprintf(stderr, "Can't open \"%s\"\n", fileName);
  58.         perror("");
  59.         exit(1);
  60.     }
  61.     }
  62.  
  63.     bufPtr = (char *)malloc(1024 * blockSize);
  64.     bzero((Address)bufPtr, 1024 * blockSize);
  65.     for (k=0 ; k<numKbytes ; k += blockSize) {
  66.     bytesWritten = write(openFileID,  bufPtr, 1024 * blockSize);
  67.     if (bytesWritten < 1024 * blockSize) {
  68.         if (bytesWritten < 0) {
  69.         perror("Write failed:");
  70.         } else  {
  71.         fprintf(stderr, "Short write (%d not %d)\n",
  72.             bytesWritten, 1024 * blockSize);
  73.         }
  74.         break;
  75.     }
  76.     }
  77.     exit(errno);
  78. }
  79.